package com.huilian.iotbox.admin.wap.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.huilian.iotbox.admin.wap.dao.NoticeMessageContentDao;
import com.huilian.iotbox.admin.wap.service.MerchantStoreService;
import com.huilian.iotbox.admin.wap.service.NoticeMessageContentService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.MerchantStoreDto;
import com.huilian.iotbox.data.dto.NoticeMessageContentDto;
import com.huilian.iotbox.data.po.MerchantStore;
import com.huilian.iotbox.data.vo.MerchantStoreVo;
import com.huilian.iotbox.data.vo.NoticeMessageContentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author 龙 路 飞
 * @CreateDate: 2021-11-13 15:56
 * @Description:
 */
@Service
public class NoticeMessageContentServiceImpl implements NoticeMessageContentService {
    @Autowired
    private NoticeMessageContentDao noticeMessageContentDao;
    @Autowired
    private MerchantStoreService merchantStoreService;

    @Override
    public List<NoticeMessageContentVo> findByUserIdList(NoticeMessageContentDto noticeMessageContentDto) {
        return noticeMessageContentDao.findByUserIdList(noticeMessageContentDto);
    }

    @Override
    public ServerResponse edit(NoticeMessageContentDto noticeMessageContentDto) {
        if(StringUtils.isEmpty(noticeMessageContentDto.getContent())){
            return ServerResponse.createByError("请输入消息内容");
        }
        if(noticeMessageContentDto.getId() > 0){
            noticeMessageContentDao.update(noticeMessageContentDto);
        }else{
            noticeMessageContentDao.insert(noticeMessageContentDto);
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse delete(NoticeMessageContentDto noticeMessageContentDto) {
        Integer result = noticeMessageContentDao.delete(noticeMessageContentDto);
        if(result > 0) {
            MerchantStore merchantStore = new MerchantStore();
            merchantStore.setUserId(noticeMessageContentDto.getUserId());
            for (Integer storeId : noticeMessageContentDto.getStoreIds()) {
                merchantStore.setId(storeId);
                MerchantStoreVo merchantStoreVo = merchantStoreService.findMerchantStoreByUserIdByStoreId(merchantStore);
                if(!ObjectUtils.isEmpty(merchantStoreVo)){
                    if(!ObjectUtils.isEmpty(merchantStoreVo.getNoticeMessageContentId()) && merchantStoreVo.getNoticeMessageContentId().equals(noticeMessageContentDto.getId())){
                        System.err.println("删除绑定ID");
                        MerchantStoreDto merchantStoreDto = new MerchantStoreDto();
                        merchantStoreDto.setId(merchantStoreVo.getId());
                        merchantStoreDto.setNoticeMessageContentId(null);
                        result = merchantStoreService.updateNmcId(merchantStoreDto);
                        if(result > 0){
                            merchantStoreDto.setNoticeMessageTagId(null);
                            result = merchantStoreService.updateNmtId(merchantStoreDto);
                        }
                    }
                }
            }
        }
        return ServerResponse.createBySuccess(result);
    }
}
